package com.qk.management.controller;


import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.domain.customer.CustomerDO;
import com.qk.dto.customer.CustomerDTO;
import com.qk.dto.customer.CustomerListDTO;
import com.qk.entity.Course;
import com.qk.entity.Customer;
import com.qk.management.service.CourseService;
import com.qk.management.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/customers")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private CourseService courseService;

    @GetMapping
    public Result listCustomer(CustomerListDTO dto){
        PageResult<CustomerDO> pageResult = customerService.listCustomer( dto);
        return Result.success(pageResult);
    }

    @PostMapping
    public Result addCustomer(@RequestBody CustomerDTO dto){
        customerService.addCustomer(dto);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getCustomerById(@PathVariable Integer id){
        Customer customer = customerService.getCustomerById(id);
        return Result.success(customer);
    }

    @PutMapping()
    public Result updateCustomer(@RequestBody Customer customer){
        customer.setUpdateTime(LocalDateTime.now());
        customerService.updateById(customer);
        return Result.success();
    }

    @GetMapping("/courses")
    public Result getCoursesList(){
        List<Course> courses = courseService.listCourses();
        return Result.success(courses);
    }

}
